﻿using System;
using System.Collections.Generic;
using Thuoc24h.Data;
using Thuoc24h.ViewModels;
using System.Linq;
using Thuoc24h.Common.Enums;


namespace Thuoc24h.Business
{
    public sealed class ReviewRepository
    {
        #region Category

        public static List<CategoryModel> GetCategoryByType(int type)
        {

            int count = 1;
            var myCategoryModels = new List<CategoryModel>();
            using (var model = new MedLifeDataContext())
            {
                var categories = model.Categories.Where(item => item.CategoryType == type).ToList();
                string parentName = "N/a";
                foreach (var category in categories)
                {
                    var parent = categories.SingleOrDefault(item => item.ParentId == category.Id);
                    if (parent != null)
                    {
                        parentName = parent.Name;
                    }
                    var categoryModel = new CategoryModel(category.Id, category.Name, category.UnsignName, category.SortOrder, parentName, count);
                    myCategoryModels.Add(categoryModel);
                    count++;
                }
            }
            return myCategoryModels;
        }

        public static List<CategoryModel> GetCategoryParent(int type)
        {


            var myCategoryModels = new List<CategoryModel>();
            using (var model = new MedLifeDataContext())
            {
                var categories = model.Categories.Where(item => item.CategoryType == type).ToList();

                foreach (var category in categories)
                {


                    var categoryModel = new CategoryModel(category.Id, category.Name, category.ParentId);
                    myCategoryModels.Add(categoryModel);
                }
            }
            return myCategoryModels;
        }
        public static bool CategoryDelete(int id)
        {
            bool sucess = false;
            using (var model = new MedLifeDataContext())
            {
                var article = model.Categories.SingleOrDefault(item => item.Id == id);

                if (article != null)
                {
                    try
                    {
                        model.Categories.DeleteObject(article);
                        int i = model.SaveChanges();
                        if (i > 0)
                        {
                            sucess = true;
                        }
                    }
                    catch (Exception exception)
                    {
                        throw new Exception(exception.Message);
                    }
                }
            }
            return sucess;
        }
        public static Category GetCategoryById(int id)
        {
            Category category;
            using (var model = new MedLifeDataContext())
            {
                category = model.Categories.SingleOrDefault(item => item.Id == id);
            }
            return category;
        }
        #endregion
        #region News
        public static bool NewsDelete(int id)
        {
            bool sucess = false;
            using (var model = new MedLifeDataContext())
            {
                var news = model.News.SingleOrDefault(item => item.Id == id);

                if (news != null)
                {
                    try
                    {
                        var relatedNews = model.RelatedNews.Where(item => item.NewsId == id);
                        if (relatedNews.Any())
                        {
                            foreach (var relatedNew in relatedNews)
                            {
                                model.RelatedNews.DeleteObject(relatedNew);
                            }
                        }

                        model.News.DeleteObject(news);
                        int i = model.SaveChanges();
                        if (i > 0)
                        {
                            sucess = true;
                        }
                    }
                    catch (Exception exception)
                    {
                        throw new Exception(exception.Message);
                    }
                }
            }
            return sucess;
        }

        public static News NewsById(int id)
        {
            News news;
            using (var model = new MedLifeDataContext())
            {
                news = model.News.SingleOrDefault(item => item.Id == id);
            }
            return news;
        }

        public static List<NewsModels> GetNewsReferences(int categoryId)
        {
            var myNewsModels = new List<NewsModels>();
            List<News> _news;
            using (var model = new MedLifeDataContext())
            {
                _news = model.News.Where(item => item.CategoryId == categoryId).ToList();

                foreach (var news in _news)
                {
                    var newsModel = new NewsModels(news.Id, news.Title);
                    myNewsModels.Add(newsModel);
                }
            }
            return myNewsModels;
        }
        public static List<NewsModels> GetNewsReferences()
        {


            var myNewsModels = new List<NewsModels>();
            using (var model = new MedLifeDataContext())
            {
                var _news = model.News.ToList();

                foreach (var news in _news)
                {
                    var newsModel = new NewsModels(news.Id, news.Title);
                    myNewsModels.Add(newsModel);
                }
            }
            return myNewsModels;
        }

        public static List<NewsModels> GetAllNews(int page, int pageSize)
        {
            var skip = 0;
            if (page > 0)
            {
                skip = page * pageSize;
            }


            var myNewsModels = new List<NewsModels>();
            List<News> _news;
            using (var model = new MedLifeDataContext())
            {
                _news = model.News.OrderByDescending(item => item.CreatedDate).Skip(skip).Take(pageSize).ToList();

                foreach (var news in _news)
                {
                    var newsModel = new NewsModels(news.Id, news.Title, news.UnsignTitle, news.Description, news.CategoryId, news.ThumbImage);
                    myNewsModels.Add(newsModel);
                }
            }
            return myNewsModels;
        }

        public static List<News> GetAllNews()
        {
            using (var model = new MedLifeDataContext())
            {
                return model.News.ToList();
            }
        }

        public static IEnumerable<NewsModels> GetNewsReferencesDetail(int newsId)
        {
            using (var model = new MedLifeDataContext())
            {
                var refenews = model.RelatedNews.Where(item => item.NewsId == newsId);
                foreach (var relatedNew in refenews)
                {
                    var news = model.News.Single(item => item.Id == relatedNew.RelatedNewsId);
                    yield return new NewsModels(news.Id, news.Title, news.UnsignTitle, news.Description, news.CategoryId, news.ThumbImage);
                }
            }
        }

        public static int GetNewsReferencesCount(int newsId)
        {
            using (var model = new MedLifeDataContext())
            {
                return model.RelatedNews.Count(item => item.NewsId == newsId);

            }
        }
        public static List<NewsModels> GetNewsCategoryId(int categoryId, int page, int pageSize)
        {

            var skip = 0;
            if (page > 0)
            {
                skip = page * pageSize;
            }
            var myNewsModels = new List<NewsModels>();
            using (var model = new MedLifeDataContext())
            {
                var _news = model.News.Where(item => item.CategoryId == categoryId).OrderByDescending(item => item.CreatedDate).Skip(skip).Take(pageSize).ToList();

                foreach (var news in _news)
                {
                    var newsModel = new NewsModels(news.Id, news.Title, news.UnsignTitle, news.Description, news.CategoryId, news.ThumbImage);
                    myNewsModels.Add(newsModel);
                }
            }
            return myNewsModels;
        }

        public static List<News> GetNewsCategoryId(int categoryId)
        {
            using (var model = new MedLifeDataContext())
            {
                return model.News.Where(item => item.CategoryId == categoryId).ToList();
            }
        }

        #endregion
        #region RelatedNews

        public static bool RelatedNewsDelete_List(int id)
        {
            bool sucess = false;
            using (var model = new MedLifeDataContext())
            {


                try
                {
                    var relatedNews = model.RelatedNews.Where(item => item.NewsId == id);
                    if (relatedNews.Any())
                    {
                        foreach (var relatedNew in relatedNews)
                        {
                            model.RelatedNews.DeleteObject(relatedNew);
                        }
                    }

                    int i = model.SaveChanges();
                    if (i > 0)
                    {
                        sucess = true;
                    }
                }
                catch (Exception exception)
                {
                    throw new Exception(exception.Message);
                }

                return sucess;
            }
        #endregion
        }
        #region Product
        public static Product GetProductById(int id)
        {
            Product product;
            using (var model = new MedLifeDataContext())
            {
                product = model.Products.SingleOrDefault(item => item.Id == id);             
                var unit = product.Unit;
                var unitPrice = model.PriceUnits.FirstOrDefault(it => it.Unit == unit);
                double? displayPrice = 0;
                double displayDiscountPrice = 0;
                double? displayDiscountPercent = 0;
                if (unitPrice != null)
                {
                    displayDiscountPrice = (double)((unitPrice.Price / 100) * (100 - unitPrice.DiscountPercent));
                    displayPrice = unitPrice.Price;
                    displayDiscountPercent = unitPrice.DiscountPercent;
                }
                else
                {
                    displayDiscountPrice = 0;
                    displayPrice = 0;
                    displayDiscountPercent = 0;
                }
                product.Price = displayPrice;
                product.DiscountPercent = displayDiscountPercent;                
            }
            return product;
        }
        public static bool ProductDelete(int id)
        {
            bool sucess = false;
            using (var model = new MedLifeDataContext())
            {
                var product = model.Products.SingleOrDefault(item => item.Id == id);

                if (product != null)
                {
                    try
                    {
                        var imagesProduct = model.ProductImages.Where(item => item.ProductId == id);
                        if (imagesProduct.Any())
                        {
                            foreach (var image in imagesProduct)
                            {
                                model.ProductImages.DeleteObject(image);
                            }
                        }

                        var priceUnitProduct = model.PriceUnits.Where(item => item.ProductId == id);
                        if (priceUnitProduct.Any())
                        {
                            foreach (var priceUnit in priceUnitProduct)
                            {
                                model.PriceUnits.DeleteObject(priceUnit);
                            }
                        }

                        model.Products.DeleteObject(product);



                        int i = model.SaveChanges();
                        if (i > 0)
                        {
                            sucess = true;
                        }



                    }
                    catch (Exception exception)
                    {
                        throw new Exception(exception.Message);
                    }
                }
            }
            return sucess;
        }
        public static bool ProductImageDelete(int id)
        {
            bool sucess = false;
            using (var model = new MedLifeDataContext())
            {
                var proimage = model.ProductImages.SingleOrDefault(item => item.Id == id);

                if (proimage != null)
                {
                    try
                    {
                        model.ProductImages.DeleteObject(proimage);
                        int i = model.SaveChanges();
                        if (i > 0)
                        {
                            sucess = true;
                        }
                    }
                    catch (Exception exception)
                    {
                        throw new Exception(exception.Message);
                    }
                }
            }
            return sucess;
        }

        public static bool ProductUnitDelete(int id)
        {
            bool sucess = false;
            using (var model = new MedLifeDataContext())
            {
                var proUnit = model.PriceUnits.SingleOrDefault(item => item.Id == id);

                if (proUnit != null)
                {
                    try
                    {
                        model.PriceUnits.DeleteObject(proUnit);
                        int i = model.SaveChanges();
                        if (i > 0)
                        {
                            sucess = true;
                        }
                    }
                    catch (Exception exception)
                    {
                        throw new Exception(exception.Message);
                    }
                }
            }
            return sucess;
        }
        public static List<ProductImage> GetProductImages(int productId)
        {
            using (var model = new MedLifeDataContext())
            {
                return model.ProductImages.Where(item => item.ProductId == productId).ToList();
            }

        }

        public static List<PriceUnit> GetProductPriceUnit(int productId)
        {
            using (var model = new MedLifeDataContext())
            {
                return model.PriceUnits.Where(item => item.ProductId == productId).ToList();
            }

        }

        public static List<ProductModels> GetProductGroupBuy(int take)
        {

            var myProductModel = new List<ProductModels>();
            List<Product> _products;
            using (var model = new MedLifeDataContext())
            {
                _products = model.Products.Where(item => item.IsGroupBuy == true).Take(take).ToList();

                foreach (var product in _products)
                {
                    double priceDes = (double)((product.Price / 100) * (100 - product.DiscountPercent));
                    var productModel = new ProductModels(product.Id, product.Name, product.UnsignName, product.ThumbImage, product.Price, priceDes, product.DiscountPercent, product.DiscountDescription, product.Description);
                    myProductModel.Add(productModel);
                }
            }
            return myProductModel;
        }

        public static List<ProductModels> GetProductGroupBuyTop1()
        {

            var myProductModel = new List<ProductModels>();
            List<Product> _products;
            using (var model = new MedLifeDataContext())
            {
                _products = model.Products.Where(item => item.IsGroupBuy == true).Take(1).ToList();

                foreach (var product in _products)
                {
                    double priceDes = (double)((product.Price / 100) * (100 - product.DiscountPercent));
                    var productModel = new ProductModels(product.Id, product.Name, product.UnsignName, product.ThumbImage, product.Price, priceDes, product.DiscountPercent, product.DiscountDescription, product.Description);
                    myProductModel.Add(productModel);
                }
            }
            return myProductModel;
        }

        public static List<ProductModels> GetProductDiscount(int take)
        {

            var myProductModel = new List<ProductModels>();
            List<Product> _products;
            using (var model = new MedLifeDataContext())
            {
                _products = model.Products.Where(item => item.IsSalesOff == true).OrderByDescending(it => it.CreatedDate).Skip(1).Take(take).ToList();

                foreach (var product in _products)
                {
                    var unit = product.Unit;
                    var unitPrice = model.PriceUnits.FirstOrDefault(it => it.Unit == unit);
                    double? displayPrice = 0;
                    double displayDiscountPrice = 0;
                    double? displayDiscountPercent = 0;
                    if (unitPrice != null)
                    {
                        displayDiscountPrice = (double)((unitPrice.Price / 100) * (100 - unitPrice.DiscountPercent));
                        displayPrice = unitPrice.Price;
                        displayDiscountPercent = unitPrice.DiscountPercent;
                    }
                    else
                    {
                        displayDiscountPrice = 0;
                        displayPrice = 0;
                        displayDiscountPercent = 0;
                    }
                    var productModel = new ProductModels(product.Id, product.Name, product.UnsignName, product.ThumbImage, displayPrice, displayDiscountPrice, displayDiscountPercent, product.DiscountDescription, product.Description);
                    myProductModel.Add(productModel);
                }
            }
            return myProductModel;
        }
        public static List<ProductModels> GetProducNew(int take)
        {

            var myProductModel = new List<ProductModels>();
            List<Product> _products;
            using (var model = new MedLifeDataContext())
            {
                _products = model.Products.OrderByDescending(item => item.CreatedDate).Take(take).ToList();

                foreach (var product in _products)
                {
                    var unit = product.Unit;
                    var unitPrice = model.PriceUnits.FirstOrDefault(it => it.Unit == unit);
                    double? displayPrice = 0;
                    double displayDiscountPrice = 0;
                    double? displayDiscountPercent = 0;
                    if (unitPrice != null)
                    {
                        displayDiscountPrice = (double)((unitPrice.Price / 100) * (100 - unitPrice.DiscountPercent));
                        displayPrice = unitPrice.Price;
                        displayDiscountPercent = unitPrice.DiscountPercent;
                    }
                    else
                    {
                        displayDiscountPrice = 0;
                        displayPrice = 0;
                        displayDiscountPercent = 0;
                    }
                    var productModel = new ProductModels(product.Id, product.Name, product.UnsignName, product.ThumbImage, displayPrice, displayDiscountPrice, displayDiscountPercent, product.DiscountDescription, product.Description);
                    myProductModel.Add(productModel);
                }
            }
            return myProductModel;
        }
        public static List<ProductModels> GetProducBanchay(int take)
        {

            var myProductModel = new List<ProductModels>();

            using (var model = new MedLifeDataContext())
            {
                var children = from p in model.Products
                               join item in model.OrderDetails on p.Id equals item.ProductId
                               orderby item.Quantity descending
                               select new
                               {
                                   p.Name,
                                   p.Id,
                                   p.UnsignName,
                                   p.ThumbImage,
                                   p.Price,
                                   p.DiscountDescription,
                                   p.Description,
                                   p.DiscountPercent,
                                   p.Unit
                               };

                //foreach (var product in children)
                //{
                //    double priceDes = (double)((product.Price / 100) * (100 - product.DiscountPercent));
                //    var productModel = new ProductModels(product.Id, product.Name, product.UnsignName, product.ThumbImage, product.Price, priceDes, product.DiscountPercent, product.DiscountDescription, product.Description);
                //    myProductModel.Add(productModel);
                //}
                foreach (var product in children)
                {
                    var unit = product.Unit;
                    var unitPrice = model.PriceUnits.FirstOrDefault(it => it.Unit == unit);
                    double? displayPrice = 0;
                    double displayDiscountPrice = 0;
                    double? displayDiscountPercent = 0;
                    if (unitPrice != null)
                    {
                        displayDiscountPrice = (double)((unitPrice.Price / 100) * (100 - unitPrice.DiscountPercent));
                        displayPrice = unitPrice.Price;
                        displayDiscountPercent = unitPrice.DiscountPercent;
                    }
                    else
                    {
                        displayDiscountPrice = 0;
                        displayPrice = 0;
                        displayDiscountPercent = 0;
                    }
                    var productModel = new ProductModels(product.Id, product.Name, product.UnsignName, product.ThumbImage, displayPrice, displayDiscountPrice, displayDiscountPercent, product.DiscountDescription, product.Description);
                    myProductModel.Add(productModel);
                }
            }
            return myProductModel;
        }


        #endregion

        #region Support Chart

        public static bool SupportDelete(int id)
        {
            bool sucess = false;
            using (var model = new MedLifeDataContext())
            {
                var support = model.SupportChats.SingleOrDefault(item => item.Id == id);

                if (support != null)
                {
                    try
                    {

                        model.SupportChats.DeleteObject(support);
                        int i = model.SaveChanges();
                        if (i > 0)
                        {
                            sucess = true;
                        }
                    }
                    catch (Exception exception)
                    {
                        throw new Exception(exception.Message);
                    }
                }
            }
            return sucess;
        }

        public static SupportChat SupportById(int id)
        {
            SupportChat supportChat;
            using (var model = new MedLifeDataContext())
            {
                supportChat = model.SupportChats.SingleOrDefault(item => item.Id == id);
            }
            return supportChat;
        }

        public static List<SupportChat> GetAllSupports()
        {

            using (var model = new MedLifeDataContext())
            {
                return model.SupportChats.OrderByDescending(item => item.Id).ToList();

            }
        }

        public static List<SupportChat> GetSupportsByType(int type)
        {

            using (var model = new MedLifeDataContext())
            {
                return model.SupportChats.Where(item => item.Type == type).OrderByDescending(item => item.Id).ToList();

            }
        }
        #endregion

        #region Configs

        public static bool ConfigDelete(int id)
        {
            bool sucess = false;
            using (var model = new MedLifeDataContext())
            {
                var support = model.Configs.SingleOrDefault(item => item.Id == id);

                if (support != null)
                {
                    try
                    {

                        model.Configs.DeleteObject(support);
                        int i = model.SaveChanges();
                        if (i > 0)
                        {
                            sucess = true;
                        }
                    }
                    catch (Exception exception)
                    {
                        throw new Exception(exception.Message);
                    }
                }
            }
            return sucess;
        }

        public static Config ConfigById(int id)
        {
            Config supportChatConfig;
            using (var model = new MedLifeDataContext())
            {
                supportChatConfig = model.Configs.SingleOrDefault(item => item.Id == id);
            }
            return supportChatConfig;
        }

        public static List<Config> GetAllConfigs()
        {
            using (var model = new MedLifeDataContext())
            {
                return model.Configs.OrderByDescending(item => item.Id).ToList();
            }
        }
        #endregion

        #region Ads

        public static bool AdsDelete(int id)
        {
            bool sucess = false;
            using (var model = new MedLifeDataContext())
            {
                var ads = model.Ads.SingleOrDefault(item => item.Id == id);

                if (ads != null)
                {
                    try
                    {

                        model.Ads.DeleteObject(ads);
                        int i = model.SaveChanges();
                        if (i > 0)
                        {
                            sucess = true;
                        }
                    }
                    catch (Exception exception)
                    {
                        throw new Exception(exception.Message);
                    }
                }
            }
            return sucess;
        }

        public static Ad AdsById(int id)
        {
            Ad ads;
            using (var model = new MedLifeDataContext())
            {
                ads = model.Ads.SingleOrDefault(item => item.Id == id);
            }
            return ads;
        }

        public static List<Ad> GetAllAds()
        {
            using (var model = new MedLifeDataContext())
            {
                return model.Ads.OrderByDescending(item => item.Id).ToList();
            }
        }
        //redony image
        public static List<Ad> GetAdsBanner(int position)
        {
            List<Ad> ads;
            using (var model = new MedLifeDataContext())
            {
                var listAds = model.Ads;
                if (position == 1)
                {
                    ads = listAds.Where(item => item.ImageUrl != string.Empty && item.PositionId == position).OrderBy(item => Guid.NewGuid()).Take(1).ToList();
                }
                else
                {
                    ads = listAds.Where(item => item.EmbedCode != string.Empty && item.PositionId == position).OrderBy(item => item.SortOrder).ToList();

                }
            }
            return ads;
        }
        #endregion

        #region contact
        public static bool ContactDelete(int id)
        {
            bool sucess = false;
            using (var model = new MedLifeDataContext())
            {
                var contact = model.Contacts.SingleOrDefault(item => item.Id == id);

                if (contact != null)
                {
                    try
                    {

                        model.Contacts.DeleteObject(contact);
                        int i = model.SaveChanges();
                        if (i > 0)
                        {
                            sucess = true;
                        }
                    }
                    catch (Exception exception)
                    {
                        throw new Exception(exception.Message);
                    }
                }
            }
            return sucess;
        }

        public static int CountUnRead(int type)
        {
            using (var model = new MedLifeDataContext())
            {
                return model.Contacts.Count(item => item.IsRead == false && item.TypeRequest == type);
            }
        }
        #endregion

        #region Slide

        public static bool SlideDelete(int id)
        {
            bool sucess = false;
            using (var model = new MedLifeDataContext())
            {
                var slide = model.Slides.SingleOrDefault(item => item.Id == id);

                if (slide != null)
                {
                    try
                    {
                        model.Slides.DeleteObject(slide);
                        int i = model.SaveChanges();
                        if (i > 0)
                        {
                            sucess = true;
                        }
                    }
                    catch (Exception exception)
                    {
                        throw new Exception(exception.Message);
                    }
                }
            }
            return sucess;
        }

        public static Slide SlideById(int id)
        {
            Slide slide;
            using (var model = new MedLifeDataContext())
            {
                slide = model.Slides.SingleOrDefault(item => item.Id == id);
            }
            return slide;
        }


        public static List<Slide> GetAllSlides()
        {
            using (var model = new MedLifeDataContext())
            {
                return model.Slides.OrderBy(item => item.Id).ToList();
            }
        }

        #endregion

        #region Customer
        public static bool CustomerDelete(int id)
        {
            bool sucess = false;
            using (var model = new MedLifeDataContext())
            {
                var customer = model.Customers.SingleOrDefault(item => item.Id == id);

                if (customer != null)
                {
                    try
                    {
                        model.Customers.DeleteObject(customer);
                        int i = model.SaveChanges();
                        if (i > 0)
                        {
                            sucess = true;
                        }
                    }
                    catch (Exception exception)
                    {
                        throw new Exception(exception.Message);
                    }
                }
            }
            return sucess;


        }
        public static Customer GetCustomerById(int id)
        {
            Customer customer;
            using (var model = new MedLifeDataContext())
            {
                customer = model.Customers.SingleOrDefault(item => item.Id == id);
            }
            return customer;
        }


        #endregion
        #region Invoice

        public static Invoice GetInvoiceById(int id)
        {
            Invoice invoice;
            using (var model = new MedLifeDataContext())
            {
                invoice = model.Invoices.SingleOrDefault(item => item.Id == id);
            }
            return invoice;
        }
        #endregion
        #region ProductImage
        public static List<ProductImage> GetImageByProduct(int id)
        {
            using (var model = new MedLifeDataContext())
            {
                return model.ProductImages.Where(item => item.ProductId == id).ToList();
            }

        }
        #endregion
    }
}
